<?php

namespace Database\Seeders;


use App\Models\UserAuth;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class EventSeeder extends Seeder
{
    /**
     * php artisan db:seed --class=EventSeeder
     *
     * @return void
     */
    public function run()
    {
        // 清空数据表 后在添加
        $table = DB::table('event');
        $table->truncate();


        // 查询 学校认证的用户
        $users = (new UserAuth())->where('auth_type', 'school')->get();
        $users = json_decode(json_encode($users), true);
        $count = count($users);
        $time  = time();

        // 填充
        for($x = 0; $x <= 70; $x++){
            // 参数
            $_uKey = mt_rand(0, $count - 1);
            $_user = $users[$_uKey];
            $_row  = $this->makeRow($_user['uid'], $_user['id'], $time);
            // 保存数据到数据库中
            $table->insert($_row);
        }

    }


    // 生成数据
    protected function makeRow($uid, $auth_id, $time)
    {
        // 赛事game,  研学study，沙龙salon
        $grades = ['game' => '赛事', 'study' => '研学', 'salon' => '沙龙'];
        $keys   = array_keys($grades);
        $_type  = $keys[mt_rand(0, 2)];
        $_name  = $grades[$_type];

        // 图片
        $images = [];
        for($x = 1; $x <= mt_rand(2, 6); $x++){
            $images[] = 'https://img.shenlanhui.com/static/head_img/face_'.rand(1, 450).'.jpg';
        }

        // 结束时间
        $_end   = mktime(mt_rand(0, 24), mt_rand(0, 60), 0, date("m"), date("d") + mt_rand(2, 20), date("Y"));
        $_price = rand(1, 99) / 100;

        // 参数
        $nums   = mt_rand(3, 7);
        $d_nums = mt_rand(30, 70);
        $_row   = [
            'uid'          => $uid,
            'auth_id'      => $auth_id,
            'event_type'   => $_type,
            'event_name'   => getChar($nums).$_name,
            'organ_price'  => $_price,
            'person_price' => $_price * 2,

            'dead_time' => $_end,
            //'start_time'  => 'required',
            //'end_time'    => 'required',

            'is_refund'   => mt_rand(0, 1),
            'city'        => '成都市',
            'address'     => getChar($nums).'路'.$d_nums.'号',
            'description' => getChar($d_nums),
            'cover'       => 'https://img.shenlanhui.com/static/head_img/face_'.rand(1, 450).'.jpg',
            'images'      => json_encode($images),
            'is_propose'  => mt_rand(0, 1),
            'verify'      => mt_rand(0, 1),
            'is_sale'     => 1,
            'ctime'       => $time,
            'mtime'       => $time,
        ];

        return $_row;
    }

}
